﻿//创建时间:  2011-8-5
//创建作者: 李城
//功能说明: CMS系统 模板文件管理
//修改:  2011-8-5
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using Newtonsoft.Json;

namespace TRPOP.CMS.Web.Admin
{
    public partial class FileSelectWork : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_strPagePermissionExtraKey = "plugin_TemplateControl";
        }
        #endregion

        #region 页面变量
        protected string m_strAction = "";
        protected string m_strPath = "";
        protected string m_strSelPath = "";
        protected string m_strExtName = "|.html|.htm|.flv|.swf|.css|.txt|.js|.bmp|.gif|.png|.jpg|.jpeg|.exe|.xml|.zip|.rar|";
        protected string m_strFolderNotShow = ".svn|_svn";
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            m_strPath = RequestHelper.RequestString("path").Replace("//", "/");
            m_strSelPath = RequestHelper.RequestString("selpath");
            if (!IsPostBack)
            {
                BindData();
            }
        }
        #endregion

        #region 页面方法
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            JavaScriptObject result = new JavaScriptObject();
            string strTempletePaht = "/" + SystemInfoHelper.getSystemInfo().CMSRoot + "/" + m_strPath + "/" + m_strSelPath;
            strTempletePaht = strTempletePaht.Replace("//", "/");
            result = FileUtil.getJsonFileList(Common.Util.GetMapPath(strTempletePaht), m_strExtName);
            if (result.ContainsKey("err"))
            {
                filelist.InnerHtml = result["err"].ToString();
            }
            else
            {
                //目录
                if (result.ContainsKey("dirlist"))
                {
                    JavaScriptObject dirlist = (JavaScriptObject)result["dirlist"];
                    foreach (string key in dirlist.Keys)
                    {
                        if (m_strFolderNotShow.IndexOf(key) < 0)
                        {
                            filelist.InnerHtml += getForder(key);
                        }
                    }
                }
                //文件
                if (result.ContainsKey("filelist"))
                {
                    JavaScriptObject flist = (JavaScriptObject)result["filelist"];
                    foreach (string key in flist.Keys)
                    {
                        JavaScriptObject jsFile = (JavaScriptObject)flist[key];
                        if (jsFile.ContainsKey("extname"))
                            filelist.InnerHtml += getFile(jsFile["name"].ToString(), jsFile["extname"].ToString());
                    }
                }
            }

        }
        /// <summary>
        /// 组合folder的html
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        protected string getForder(string name)
        {
            return string.Format(@"
                                    <div class={0}work_fordertemplate{0} title={0}{1}{0}  onclick={0}TaggleCSS(this,'folder','{1}'){0}  ondblclick={0}opendir('{1}'){0}  >
                                        <TABLE border={0}0{0} cellpadding={0}0{0} cellspacing={0}2{0}>
                                            <TR>
                                                <TD ><img src={0}{2}App_Themes/{4}/images/xp_icon/{3}.png{0} alt={0}{1}{0}  ></TD>
                                                <TD>
                                                    <div   class={0}imageholder{0} >{1}</div>
                                                </TD>
                                            </TR>
                                        </TABLE>
                                    </div>
                                ",
                                   "\"", name, SystemInfoHelper.getSystemInfo().CMSRoot, GetFolderPngByFolderName(name), TRPOP.CMS.Model.SystemInfoHelper.getSystemInfo().CMSThemes);
        }
        /// <summary>
        /// 组合file的html
        /// </summary>
        /// <param name="name"></param>
        /// <param name="extname"></param>
        /// <returns></returns>
        private string getFile(string name, string extname)
        {
            string strondblclick = string.Empty;
            if (extname.ToLower() == "html" || extname.ToLower() == "htm" ||
                extname.ToLower() == "css" || extname.ToLower() == "txt" ||
                extname.ToLower() == "js" || extname.ToLower() == "xml"
                )
            {
                strondblclick = "ondblclick=\"filedblclick('" + name + "');\"";
            }
            return string.Format(@" <div class={0}work_fordertemplate{0} title={0}{1}{0} onclick={0}TaggleCSS(this,'file','{1}'){0}  {4}  >
                                        <TABLE border={0}0{0} cellpadding={0}0{0} cellspacing={0}2{0}>
                                            <TR>
                                                <TD><img src={0}{2}App_Themes/{5}/images/xp_icon/{3}.png{0} alt={0}{1}{0} ></TD>
                                                <TD>
                                                    <div class={0}imageholder{0} >{1}</div>
                                                </TD>
                                            </TR>
                                        </TABLE>
                                    </div>
                                                
                                  ", "\"", name, SystemInfoHelper.getSystemInfo().CMSRoot, getExtName(extname), strondblclick, TRPOP.CMS.Model.SystemInfoHelper.getSystemInfo().CMSThemes);
        }
        /// <summary>
        /// 根据文件夹名获取图表名
        /// </summary>
        /// <param name="strFolderName"></param>
        /// <returns></returns>
        private string GetFolderPngByFolderName(string strFolderName)
        {
            string strReturn = string.Empty;
            switch (strFolderName.ToLower())
            {
                case "img":
                case "image":
                case "images":
                    strReturn = "imagefolder";
                    break;
                case "deletedfiles":
                    strReturn = "Deletedfolder";
                    break;
                default:
                    strReturn = "folder";
                    break;
            }
            return strReturn;
        }
        /// <summary>
        /// 根据后缀名获取图标（文件）
        /// </summary>
        /// <param name="extname"></param>
        /// <returns></returns>
        private string getExtName(string extname)
        {
            string str = "";
            switch (extname.ToLower())
            {
                case "html":
                case "htm":
                    str = "html";
                    break;

                case "flv":
                case "swf":
                    str = "flash";
                    break;

                case "css":
                    str = "css";
                    break;
                case "txt":
                    str = "txt";
                    break;
                case "js":
                    str = "script";
                    break;

                case "bmp":
                case "gif":
                case "png":
                case "jpg":
                case "jpeg":
                    str = "img";
                    break;

                case "exe":
                    str = "application";
                    break;

                case "zip":
                case "rar":
                    str = "zip";
                    break;

                case "xml":
                    str = "xml";
                    break;

                default:
                    str = "file";
                    break;
            }
            return str;
        }
        #endregion
    }
}
